package com.example.modelrepositorydemo.Service;

import org.springframework.stereotype.Service;

@Service
public class ArtifactReleaseServiceImpl implements ArtifactReleaseService {

    private final ArtifactRepository artifactRepository;
    private final ReleaseRepository releaseRepository;

    public ArtifactReleaseServiceImpl(ArtifactRepository artifactRepository, ReleaseRepository releaseRepository) {
        this.artifactRepository = artifactRepository;
        this.releaseRepository = releaseRepository;
    }

    @Override
    public void createArtifactRelease(String groupName, String artifactName, String version) {
        Artifact artifact = artifactRepository.findByGroupNameAndName(groupName, artifactName);
        if (artifact == null) {
            throw new ArtifactNotFoundException("Artifact with group name " + groupName + " and name " + artifactName + " not found.");
        }
        Release release = new Release();
        release.setArtifact(artifact);
        release.setVersion(version);
        releaseRepository.save(release);
    }
}